package com.swallow.auth.domain.auth.model;

import com.swallow.auth.common.constants.RedisConstants;
import com.swallow.auth.common.enums.ErrorCode;
import com.swallow.auth.common.exception.BizException;
import com.swallow.auth.common.utils.AESUtils;
import com.swallow.auth.common.utils.SpringUtils;
import com.swallow.auth.domain.auth.repository.repository.IAuthDomainRepository;
import com.swallow.auth.domain.uniqno.NumWorker;
import com.swallow.auth.domain.uniqno.model.entity.BizNoValObj;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author: yangjie.deng@resico.cn
 * @since: 2024-05-13 10:22:18
 * @version: v1.0.0
 * @describe:
 */
@Slf4j
@Data
public class AccountEntity {
    /**
     *  唯一账户编号
     */
    private String accountNo;

    /**
     *  手机号
     */
    private String mobile;
    /**
     *  短信验证码
     */
    private String captcha;
    /**
     *  用户名
     */
    private String username;
    /**
     *  密码
     */
    private String password;
    /**
     *  注册时间
     */
    private LocalDateTime registerTime;

    /**
     * 账号状态  1-有效 2-失效
     */
    private Integer accountStatus;

    /**
     *  账号锁定状态 账号锁定状态 0-未锁定 1-锁定
     */
    private Integer accountLockStatus;

    /**
     *  删除标识
     */
    private Long deleteFlag;


    public void registerInit() {
        setAccountNo(NumWorker.bizNo(BizNoValObj.ACCOUNT_NO));
        setAccountStatus(1);
        setRegisterTime(LocalDateTime.now());
        setAccountLockStatus(0);
        setDeleteFlag(0L);
    }

    /**
     *  重复校校验
     */
    public void dupCheck() {
        IAuthDomainRepository repository = SpringUtils.getBean(IAuthDomainRepository.class);
        Map<String, Boolean> registerMap = repository.dupCheck(this);

        if (!CollectionUtils.isEmpty(registerMap)) {
            for (Map.Entry<String, Boolean> entry : registerMap.entrySet()) {
                throw new BizException(ErrorCode.REGISTER_DUP_CHECK_ERROR, entry.getKey());
            }
        }
    }

    /**
     *  验签异常
     *
     * @param aesKey 加密key
     * @param aesIv  加密value
     */
    public void pwdSignCheck(String aesKey, String aesIv) {
        if (StringUtils.isNotBlank(getPassword())) {
            try {
                String decodePwd = AESUtils.AESCBCDecrypt(getPassword(), aesKey, aesIv);
                setPassword(decodePwd);
            } catch (Exception e) {
                log.error("密码验证异常", e);
                throw new BizException(ErrorCode.SIGN_CHECK_ERROR, "密码");
            }
        }
    }

    public void captchaCheck() {
        SmsType type = SmsType.REGISTER_SMS_TYPE;

        String cacheKey = RedisConstants.SMS_CAPTCHA_KEY.formatted(type.getValue(), getMobile());
        // todo from redis
        String captcha = getCaptcha();

        if (!StringUtils.equals(getCaptcha(), captcha)) {
            throw new BizException(ErrorCode.CAPTCHA_CHECK_ERROR, "短信");
        }
    }

    /**
     *  密码强度校验
     */
    public void pwdStrengthCheck() {
        String rule = RuleEnum.PWD_STRENGTH_RULE.getRule();

        if (!getPassword().matches(rule)) {
            throw new BizException(ErrorCode.PWD_STRENGTH_ERROR);
        }
    }

    /**
     *  创建
     */
    public void create() {
        SpringUtils.getBean(IAuthDomainRepository.class).accountCreate(this);
    }

    public void encryptPwd() {
        String encrypt = SpringUtils.getBean(IAuthDomainRepository.class).encryptPwd(getPassword());
        setPassword(encrypt);
    }
}
